# *****************************************************************************
# elbert.malone/centos-hadoop-ha-cluster
#
# CentOS-7.2.1511
#
# *****************************************************************************
FROM centos:7.2.1511

MAINTAINER elbert malone <elbert.lau@qq.com>

WORKDIR /root

# -----------------------------------------------------------------------------
# 1. Import the RPM GPG keys for Repositories
#------------------------------------------------------------------------------

RUN rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7 \
	&& rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 \
	&& rpm --import https://dl.iuscommunity.org/pub/ius/IUS-COMMUNITY-GPG-KEY

# -----------------------------------------------------------------------------
# 2. Base Install
# -----------------------------------------------------------------------------
RUN rpm --rebuilddb \
	&& yum -y install \
	centos-release-scl \
	centos-release-scl-rh \
	epel-release \
	which \
	https://centos7.iuscommunity.org/ius-release.rpm \
	vim-minimal-7.4.160-1.el7 \
	sudo-1.8.6p7-17.el7_2 \
	openssh-6.6.1p1-25.el7_2 \
	openssh-server-6.6.1p1-25.el7_2 \
	openssh-clients-6.6.1p1-25.el7_2 \
	python-setuptools-0.9.8-4.el7 \
	yum-plugin-versionlock-1.1.31-34.el7 \
	&& yum versionlock add \
	vim-minimal \
	sudo \
	openssh \
	openssh-server \
	openssh-clients \
	python-setuptools \
	yum-plugin-versionlock \
	&& rm -rf /var/cache/yum/* \
	&& yum clean all

# -----------------------------------------------------------------------------
# 3. Install supervisord (required to run more than a single process in a container)
# Note: EPEL package lacks /usr/bin/pidproxy
# We require supervisor-stdout to allow output of services started by 
# supervisord to be easily inspected with "docker logs".
# -----------------------------------------------------------------------------
RUN easy_install 'supervisor == 3.2.3' 'supervisor-stdout == 0.1.1' \
	&& mkdir -p /var/log/supervisor/

ADD config/supervisor/supervisord.conf /etc/supervisord.conf

# -----------------------------------------------------------------------------
# 4. UTC Timezone & Networking
# -----------------------------------------------------------------------------
RUN ln -sf /usr/share/zoneinfo/UTC /etc/localtime \
	&& echo "NETWORKING=yes" > /etc/sysconfig/network

# -----------------------------------------------------------------------------
# 5. Configure SSH
# -----------------------------------------------------------------------------
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' \
	&& ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' \
	&& ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N '' \
	&& sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config \
	&& sed -i "s/UsePAM.*/UsePAM yes/g" /etc/ssh/sshd_config \
	&& ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa \
	&& cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys \
	&& chmod 0600 ~/.ssh/authorized_keys

# -----------------------------------------------------------------------------
# 6. Change root's password
# -----------------------------------------------------------------------------
RUN echo "root:hadoop" | chpasswd

# -----------------------------------------------------------------------------
# 7. Purge
# -----------------------------------------------------------------------------
RUN rm -rf /etc/ld.so.cache \ 
	; rm -rf /sbin/sln \
	; rm -rf /usr/{{lib,share}/locale,share/{man,doc,info,gnome/help,cracklib,il8n},{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive} \
	; rm -rf /{root,tmp,var/cache/{ldconfig,yum}}/* \
	; > /etc/sysconfig/i18n

# -----------------------------------------------------------------------------
# 8. Install Oracle JDK
# -----------------------------------------------------------------------------
# ADD Packages/jdk-8u91-linux-x64.tar.gz /usr/local

RUN cd /usr/local \
	&& curl -LO "http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jdk-8u91-linux-x64.tar.gz" -H 'Cookie: oraclelicense=accept-securebackup-cookie' \
	&& tar -zxvf jdk-8u91-linux-x64.tar.gz \
	&& rm -f jdk-8u91-linux-x64.tar.gz

ENV JAVA_HOME=/usr/local/jdk1.8.0_91
ENV PATH=$PATH:$JAVA_HOME/bin

# -----------------------------------------------------------------------------
# 9. Install Zookeeper 3.4.8
# -----------------------------------------------------------------------------
RUN cd /opt \
	&& curl -LO "http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz" \
	&& tar -xzvf zookeeper-3.4.8.tar.gz \
	&& rm -f zookeeper-3.4.8.tar.gz

ADD config/myid.sh /opt/zookeeper-3.4.8/bin/myid.sh
RUN chmod +x /opt/zookeeper-3.4.8/bin/myid.sh

ENV ZOOKEEPER_HOME=/opt/zookeeper-3.4.8
ENV PATH=$PATH:$ZOOKEEPER_HOME/bin

# -----------------------------------------------------------------------------
# 10. Install Hadoop 2.7.2
# -----------------------------------------------------------------------------
# ADD Packages/hadoop-2.7.2.tar.gz /opt

RUN cd /opt \
	&& curl -LO "http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.7.2/hadoop-2.7.2.tar.gz" \
	&& tar -xzvf hadoop-2.7.2.tar.gz \
	&& rm -f hadoop-2.7.2.tar.gz

ENV HADOOP_HOME=/opt/hadoop-2.7.2
ENV HADOOP_PREFIX=/opt/hadoop-2.7.2
ENV PATH=$PATH:$HADOOP_PREFIX/bin:$HADOOP_PREFIX/sbin
ENV HADOOP_MAPRED_HOME=$HADOOP_PREFIX
ENV HADOOP_COMMON_HOME=$HADOOP_PREFIX
ENV HADOOP_HDFS_HOME=$HADOOP_PREFIX
ENV YARN_HOME=$HADOOP_PREFIX
ENV HADOOP_CONF_DIR=$HADOOP_PREFIX/etc/hadoop
ENV YARN_CONF_DIR=$HADOOP_PREFIX/etc/hadoop
ENV HADOOP_LIBEXEC_DIR=$HADOOP_COMMON_HOME/libexec

RUN sed -i '/^export JAVA_HOME/ s:.*:export JAVA_HOME=/usr/local/jdk1.8.0_91\nexport HADOOP_PREFIX=/opt/hadoop-2.7.2\nexport HADOOP_HOME=/opt/hadoop-2.7.2\n:' $HADOOP_PREFIX/etc/hadoop/hadoop-env.sh
RUN sed -i '/^export HADOOP_CONF_DIR/ s:.*:export HADOOP_CONF_DIR=/opt/hadoop-2.7.2/etc/hadoop/:' $HADOOP_PREFIX/etc/hadoop/hadoop-env.sh

RUN mkdir -p ~/hdfs/namenode && \ 
    mkdir -p ~/hdfs/datanode && \
    mkdir -p ~/hdfs/journalnode && \
    mkdir -p ~/hdfs/temp && \
    mkdir -p ~/zookeeper && \
    mkdir $HADOOP_HOME/logs

ADD config/zookeeper/zoo.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
ADD config/ssh/ssh_config .ssh/config
# ADD config/hadoop/hadoop-env.sh $HADOOP_HOME/etc/hadoop/hadoop-env.sh
ADD config/hadoop/hdfs-site.xml $HADOOP_HOME/etc/hadoop/hdfs-site.xml 
ADD config/hadoop/core-site.xml $HADOOP_HOME/etc/hadoop/core-site.xml
ADD config/hadoop/mapred-site.xml $HADOOP_HOME/etc/hadoop/mapred-site.xml
ADD config/hadoop/yarn-site.xml $HADOOP_HOME/etc/hadoop/yarn-site.xml
ADD config/hadoop/slaves $HADOOP_HOME/etc/hadoop/slaves
ADD config/start-hadoop.sh start-hadoop.sh
ADD config/run-wordcount.sh run-wordcount.sh

RUN chmod +x ~/start-hadoop.sh && \
    chmod +x ~/run-wordcount.sh && \
    chmod +x $HADOOP_HOME/sbin/start-dfs.sh && \
    chmod +x $HADOOP_HOME/sbin/start-yarn.sh 

# format namenode
# RUN $HADOOP_HOME/bin/hdfs namenode -format

# EXPOSE 22

CMD ["/usr/bin/supervisord", "--configuration=/etc/supervisord.conf"]
